//------------------------------------------------------------------------------
//SFWK Framework
//Copyright (C) 2011 SQLI

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SFWK.Core.Security
{
    /// <summary>
    /// Provides contextual security permission check, depending on current entitie
    /// </summary>
    /// <typeparam name="TEntity">Specific check on an entity</typeparam>
    /// <typeparam name="TKey">Specific check providing a primary key of an entity</typeparam>
    public abstract class   SFWKContextualOperation<TEntity, TKey> : SFWKOperation 
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public SFWKContextualOperation()
        {
        }

        /// <summary>
        /// Checks the contextual permission on Entity or providing the primary key of an entity
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public sealed override bool HasPermissionForContext(object obj)
        {
            if (obj == null) return base.HasPermission();
            if (obj.GetType().Equals(typeof(TKey))) return HasPermission((TKey)obj);
            if (obj.GetType().Equals(typeof(TEntity)) || obj is TEntity) return HasPermission((TEntity)obj);
            return false;
        }

        /// <summary>
        /// Checks permission related to the passed primary key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public abstract bool HasPermission(TKey key);

        /// <summary>
        /// Checks permission related to the passed entity
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public abstract bool HasPermission(TEntity entity);
        
    }
}

